1 /***
2 * Copyright 2003, 2004, 2005. CodeStreet LLC.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 package com.codestreet.selector.parser;
16
17 import java.util.Map;
18
19 /***
20 * Class to represent the <tt>BETWEEN</tt> operator. Immutable.
21 *
22 * @author Jawaid Hakim.
23 */
24 class OpBETWEEN implements IExpressionNumeric
25 {
26 /***
27 * Ctor.
28 *
29 * @param val
30 * Test value.
31 * @param lower
32 * Lower limit (inclusive).
33 * @param upper
34 * Upper limit (inclusive).
35 */
36 public OpBETWEEN(final IExpression val, final IExpression lower,
37 final IExpression upper)
38 {
39 val_ = val;
40 lower_ = lower;
41 upper_ = upper;
42 }
43
44 public Object eval(final Map identifiers)
45 {
46 Object oVal = val_.eval(identifiers);
47 Object oLower = lower_.eval(identifiers);
48 Object oUpper = lower_.eval(identifiers);
49
50 return eval(oVal, oLower, oUpper);
51 }
52
53 /***
54 * Evaluate the expression.
55 *
56 * @param provider
57 * Value provider. During the evaluation of the selector a
58 * callback is made to the value provider to get identifier
59 * values.
60 * @param corr
61 * Correlation data. This data is as-is passed to the provider.
62 * @return Returns the result of the expression evaluation.
63 */
64 public Object eval(final IValueProvider provider, final Object corr)
65 {
66 Object oVal = val_.eval(provider, corr);
67 Object oLower = lower_.eval(provider, corr);
68 Object oUpper = lower_.eval(provider, corr);
69
70 return eval(oVal, oLower, oUpper);
71 }
72
73 private Object eval(final Object oVal, final Object oLower,
74 final Object oUpper)
75 {
76
77 if (!(oVal instanceof NumericValue)
78 || !(oLower instanceof NumericValue)
79 || !(oUpper instanceof NumericValue))
80 return Result.RESULT_UNKNOWN;
81
82 double val = ((NumericValue) oVal).doubleValue();
83
84 double lower = ((NumericValue) oLower).doubleValue();
85 if (val < lower)
86 return Result.RESULT_FALSE;
87
88 double upper = ((NumericValue) oUpper).doubleValue();
89 return (val <= upper) ? Result.RESULT_TRUE : Result.RESULT_FALSE;
90 }
91
92 public String toString()
93 {
94 StringBuffer buf = new StringBuffer(val_.toString() + " BETWEEN ");
95 buf.append(lower_.toString()).append(" AND ").append(upper_.toString());
96 return buf.toString();
97 }
98
99 private final IExpression val_;
100
101 private final IExpression lower_;
102
103 private final IExpression upper_;
104 }
This page was automatically generated by Maven